<?php
namespace app\controller;
use app\model\Am;
use app\model\AmForecast;
use app\model\AmForecast as AmForecastModel;
use app\model\Bjpk;
use app\model\BjpkForecast;
use app\model\BjpkForecast as BjpkForecastModel;
use app\model\Hk;
use app\model\HkForecast;
use app\model\HkForecast as HkForecastModel;
use app\model\Jsks;
use think\Request;
use think\facade\Filesystem;
use app\model\Cat as CatModel;
use app\model\Jnd as JndModel;
use app\model\JndForecast as JndForecastModel;
use app\model\Bjpk as BjpkModel;
use app\model\Jsks as JsksModel;
use app\model\JsksForecast as JsksForecastModel;
use app\model\Hk as HkModel;
use app\model\Am as AmModel;
use app\controller\Mobile as MobileController;

class Index extends MobileController
{
    // 预测页面

    public function index(Request $request)
    {
        $data   =   $request->param();
        $cat = CatModel::select();
        $mobile = $this->isMobile(); //实例化该方法
        if($mobile=="true"){
            if(!$request->has('cat_id','get')){
                $cat2 = CatModel::find(6);
                $adv=\app\model\Adv::select();
                $info=CatModel::hasWhere('info',['cat_id'=>6])->with('info')->select();
                return view('mobile', [
                    'cat'   =>  $cat,
                    'cat2'  =>  $cat2,
                    'adv'   => $adv->toArray(),
                    'info'  =>  $info->toArray(),
                ]);
            }else{
                $cat2 = CatModel::find($data['cat_id']);
                $adv=CatModel::hasWhere('adv',['cat_id'=>$data['cat_id']])->with('adv')->select();
                $info=CatModel::hasWhere('info',['cat_id'=>$data['cat_id']])->with('info')->select();
                $ycdata=[];
                if($data['cat_id']=='6'){
                    // 香港六合彩
                    $kjdata = HkModel::limit(20)->order('expect desc')->select();
                    for ($i=0;$i<4;$i++){
                        $ycdata[$i] = HkForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                    }
                }
                if($data['cat_id']=='8'){
                    // 澳门六合彩
                    $kjdata = AmModel::limit(20)->order('expect desc')->select();
                    for ($i=0;$i<4;$i++){
                        $ycdata[$i] = AmForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                    }
                }
                if($data['cat_id']=='9'){
                    // 北京pk10
                    $kjdata = BjpkModel::limit(20)->order('expect desc')->select();
                    for ($i=0;$i<2;$i++){
                        $ycdata[$i] = BjpkForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                    }
                }
                if($data['cat_id']=='10'){
                    // 江苏快三
                    $kjdata = JsksModel::limit(20)->order('expect desc')->select();
                    for ($i=0;$i<3;$i++){
                        $ycdata[$i] = JsksForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                    }
                }
                if($data['cat_id']=='11'){
                    // 加拿大
                    $kjdata = JndModel::limit(20)->order('expect desc')->select();
                    for ($i=0;$i<3;$i++){
                        $ycdata[$i] = JndForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                    }
                }
                $restime = strtotime($kjdata[0]['opentime'])-strtotime($kjdata[1]['opentime']);
                $rrtime = $restime+strtotime($kjdata[0]['opentime']);
                $rtime = date('Y-m-d h:i:s',$rrtime);
                $downtime = strtotime($rtime) - strtotime(date('Y-m-d h:i:s'));
                return view('plan', [
                    'cat_id'    =>  $data['cat_id'],
                    'cat'   =>  $cat,
                    'cat2'  =>  $cat2,
                    'adv'   => $adv->toArray(),
                    'info'  =>  $info->toArray(),
                    'kjdata'    =>  $kjdata->toArray(),
                    'ycdata'    =>  $ycdata,
                    'downtime'  =>  $downtime
                ]);
            }

        }else{
            if(!$request->has('cat_id','get')){
                $data['cat_id']=$cat[0]['id'];
            }
            $cat2 = CatModel::find($data['cat_id']);
            $adv=CatModel::hasWhere('adv',['cat_id'=>$data['cat_id']])->with('adv')->select();
            $info=CatModel::hasWhere('info',['cat_id'=>$data['cat_id']])->with('info')->select();
            $ycdata=[];
            if($data['cat_id']=='6'){
                // 香港六合彩
                $kjdata = HkModel::limit(20)->order('expect desc')->select();
                for ($i=0;$i<4;$i++){
                    $ycdata[$i] = HkForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
            }
            if($data['cat_id']=='8'){
                // 澳门六合彩
                $kjdata = AmModel::limit(20)->order('expect desc')->select();
                for ($i=0;$i<4;$i++){
                    $ycdata[$i] = AmForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
            }
            if($data['cat_id']=='9'){
                // 北京pk10
                $kjdata = BjpkModel::limit(20)->order('expect desc')->select();
                for ($i=0;$i<2;$i++){
                    $ycdata[$i] = BjpkForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
            }
            if($data['cat_id']=='10'){
                // 江苏快三
                $kjdata = JsksModel::limit(20)->order('expect desc')->select();
                for ($i=0;$i<3;$i++){
                    $ycdata[$i] = JsksForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
            }
            if($data['cat_id']=='11'){
                // 加拿大
                $kjdata = JndModel::limit(20)->order('expect desc')->select();
                for ($i=0;$i<3;$i++){
                    $ycdata[$i] = JndForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
            }
            $restime = strtotime($kjdata[0]['opentime'])-strtotime($kjdata[1]['opentime']);
            $rrtime = $restime+strtotime($kjdata[0]['opentime']);
            $rtime = date('Y-m-d h:i:s',$rrtime);
            $downtime = strtotime($rtime) - strtotime(date('Y-m-d h:i:s'));
            return view('index', [
                'cat_id'    =>  $data['cat_id'],
                'cat'   =>  $cat,
                'cat2'  =>  $cat2,
                'adv'   => $adv->toArray(),
                'info'  =>  $info->toArray(),
                'kjdata'    =>  $kjdata->toArray(),
                'ycdata'    =>  $ycdata,
                'downtime'  =>  $downtime
            ]);
        }
    }

    public function plan(Request $request){
        $data   =   $request->param();
        $res =['code' => 1];
        if ($data['code']==6){
            // HK
            $res['opendata'] = HkModel::limit(20)->order('expect desc')->select();
            $res['plan']  =  HkForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
            $res['type'] = 'og1lhc';
        }
        if ($data['code']==8){
            // amyc
            $res['opendata'] = AmModel::limit(20)->order('expect desc')->select();
            $res['plan']  =  AmForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
            $res['type'] = 'og1lhc';
        }
        if ($data['code']==9){
            // bjpk10
            $res['opendata'] = BjpkModel::limit(20)->order('expect desc')->select();
            $res['plan']  =  BjpkForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
            $res['type'] = 'jspk10';
        }
        if ($data['code']==10){
            // jsks
            $res['opendata'] = JsksModel::limit(20)->order('expect desc')->select();
            $res['plan']  =  JsksForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
            $res['type'] = 'og1k3';
        }
        if ($data['code']==11){
            // jnd
            $res['opendata'] = JndModel::limit(20)->order('expect desc')->select();
            $res['plan']  =  JndForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
            $res['type'] = 'ff28';
        }
        $restime = strtotime($res['opendata'][0]['opentime'])-strtotime($res['opendata'][1]['opentime']);
        $rrtime = $restime+strtotime($res['opendata'][0]['opentime']);
        $rtime = date('Y-m-d h:i:s',$rrtime);
        $downtime = strtotime($rtime) - strtotime(date('Y-m-d h:i:s'));
        $res['downtime'] = $downtime;
        return $res;
    }


    // 接口
    public function getdata(Request $request){
        $data   =   $request->param();

        if(!$request->has('url','get')){
            $data['url'] = 'hk';
        }

        if(!$request->has('plan','get')){
            $data['plan'] = 0;
        }
        if(!$request->has('host','get')){
            // 不存在
            $resdata['code'] = 201;
            $resdata['msg'] = '域名未添加';
        }
        $res = $data['host'];
        // 开始获取域名的ddd
//        dd($request);
//        $host   =   $request->server();
        if(strpos($res,'https://')){
            $resr = str_ireplace('https://','',$res);
        }else{
            $resr = str_ireplace('http://','',$res);
        }
        $host = \app\model\Host::where('host', $resr)->find();
        if ($host){
            // 存在
            $cat = \app\model\Hostcat::where(['host_id'=>$host['id']])->select();
            $cat2 = \app\model\Hostcat::where(['host_id'=>$host['id'],'url'=>$data['url']])->select();
            $info = \app\model\Hostcat::hasWhere('hostinfo',['hostcat_id'=>$cat2[0]['id']])->with('hostinfo')->select();
            $adv = \app\model\Hostcat::hasWhere('hostadv',['hostcat_id'=>$cat2[0]['id']])->with('hostadv')->select();
            $infojson = $info->toArray();
            $advjson = $adv->toArray();
            if ($advjson){
                $resdata['adv'] =$advjson[0]['hostadv'];
            }else{
                $resdata['adv'] =[];
            }
            $resdata['info'] = $infojson[0]['hostinfo'];
            $resdata['cat'] = $cat;
            $resdata['cat2'] = $cat2;
            if ($data['url'] == 'hk'){
                // 香港开奖预测
                $resdata['opendata'] = HkModel::limit(20)->order('expect desc')->select();
                $resdata['plan']  =  HkForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
                for ($i=0;$i<4;$i++){
                    $ycdata[$i] = HkForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
                $resdata['planall'] = $ycdata;
            }
            if ($data['url'] == 'am'){
                // 澳门开奖预测
                $resdata['opendata'] = AmModel::limit(20)->order('expect desc')->select();
                $resdata['plan']  =  AmForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
                for ($i=0;$i<4;$i++){
                    $ycdata[$i] = AmForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
                $resdata['planall'] = $ycdata;
            }
            if ($data['url'] == 'bjpk'){
                // 北京pk10开奖预测
                $resdata['opendata'] = BjpkModel::limit(20)->order('expect desc')->select();
                $resdata['plan']  =  BjpkForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
                for ($i=0;$i<2;$i++){
                    $ycdata[$i] = BjpkForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
                $resdata['planall'] = $ycdata;
            }
            if ($data['url'] == 'jsks'){
                // 江苏快三开奖预测
                $resdata['opendata'] = JsksModel::limit(20)->order('expect desc')->select();
                $resdata['plan']  =  JsksForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
                for ($i=0;$i<3;$i++){
                    $ycdata[$i] = JsksForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
                $resdata['planall'] = $ycdata;
            }
            if ($data['url'] == 'jnd'){
                // 加拿大28开奖预测
                $resdata['opendata'] = JndModel::limit(20)->order('expect desc')->select();
                $resdata['plan']  =  JndForecastModel::where('forecast_type',$data['plan']+1)->limit(20)->order('id desc')->select();
                for ($i=0;$i<3;$i++){
                    $ycdata[$i] = JndForecastModel::where('forecast_type',$i+1)->limit(20)->order('start_issue desc')->select();
                }
                $resdata['planall'] = $ycdata;
            }
            $restime = strtotime($resdata['opendata'][0]['opentime'])-strtotime($resdata['opendata'][1]['opentime']);
            $rrtime = $restime+strtotime($resdata['opendata'][0]['opentime']);
            $rtime = date('Y-m-d h:i:s',$rrtime);
            $downtime = strtotime($rtime) - strtotime(date('Y-m-d h:i:s'));
            $resdata['downtime'] = $downtime;
            $resdata['code'] = 200;
        }else{
            // 不存在
             $resdata['code'] = 201;
             $resdata['msg'] = '域名未添加';
        }
//        dd($cat->toArray());'
        return  json($resdata);
    }
    // 上传图片
    public function upload(){
        $file = request()->file('image');
        // 移动到框架应用根目录/uploads/ 目录下
        try{
            // 上传图片到本地服务器
            $saveName = Filesystem::disk('public')->putFile('img',$file);
            $arr = ['code' => 200, 'msg' => '图片上传成功！', 'path' =>  $saveName];
            return json($arr);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->exceptionHandle($e,'图片上传失败!' . $e->getMessage(),'json','');
        }
    }

}
